package com.cxrj.zhsq.userManage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cxrj.zhsq.userManage.dao.OrganizationMapper;
import com.cxrj.zhsq.userManage.dao.UserMapper;
import com.cxrj.zhsq.userManage.entity.Organization;
import com.cxrj.zhsq.userManage.service.OrganizationService;

@Service
public class OrganizationServiceImpl implements OrganizationService {
	@Autowired
	private OrganizationMapper organizationMapper;

	@Resource
	private UserMapper userMapper;

	@Override
	public Map<String, Object> insertOrganization(Organization organization) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (organization.getOrganizationId() != null) {
			c = organizationMapper.updateByPrimaryKeySelective(organization);
		} else {
			organization.setParentId(1);
			c = organizationMapper.insertSelective(organization);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectAllCommunityList(String isApp) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("isApp", isApp);
		List<Map<String, Object>> list = organizationMapper.selectAllCommunity(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectAllCommunityByName(String name) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("name", name);
		resultMap.put("list", organizationMapper.selectByName(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectOrganizationByPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("name", request.getParameter("name"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = organizationMapper.selectByCount(sqlMap) + "";
		List<Organization> list = organizationMapper.selectOrganizationByPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectOrganizationById(Integer id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("organization", organizationMapper.selectByPrimaryKey(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> updateOrganization(Organization organization) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = organizationMapper.updateByPrimaryKeySelective(organization);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}
}
